<template>
  <div id="userLoginPage">
    <h2 class="title">Codemi - 用户登录</h2>
    <div class="desc">不写一行代码，生成完整应用</div>
    <a-form :model="formState" name="basic" autocomplete="off" @finish="handleSubmit">
      <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入账号' }]">
        <a-input v-model:value="formState.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item
        name="userPassword"
        :rules="[
          { required: true, message: '请输入密码' },
          { min: 8, message: '密码长度不能小于 8 位' },
        ]"
      >
        <a-input-password v-model:value="formState.userPassword" placeholder="请输入密码" />
      </a-form-item>
      <div class="tips">
        没有账号
        <RouterLink to="/user/register">去注册</RouterLink>
      </div>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">登录</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { userLogin } from '@/api/userController.ts'
import { useLoginUserStore } from '@/stores/loginUser.ts'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'

const formState = reactive<API.UserLoginRequest>({
  userAccount: '',
  userPassword: '',
})

const router = useRouter()
const loginUserStore = useLoginUserStore()

/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: any) => {
  try {
    const res = await userLogin(values)
    // 登录成功，把登录态保存到全局状态中
    if (res.data.code === 0 && res.data.data) {
      // 尝试获取用户信息，即使失败也不影响登录流程
      try {
        await loginUserStore.fetchLoginUser()
      } catch (error) {
        console.error('获取用户信息失败:', error)
        // 如果获取用户信息失败，使用登录接口返回的用户信息
        if (res.data.data) {
          loginUserStore.setLoginUser(res.data.data)
        }
      }
      message.success('登录成功')
      // 延迟一下再跳转，确保消息显示
      setTimeout(() => {
        router.push({
          path: '/',
          replace: true,
        })
      }, 300)
    } else {
      message.error('登录失败，' + res.data.message)
    }
  } catch (error: any) {
    console.error('登录失败:', error)
    message.error('登录失败，' + (error?.response?.data?.message || error?.message || '网络错误'))
  }
}
</script>

<style scoped>
#userLoginPage {
  min-height: 100vh;
  background:
    linear-gradient(180deg, #0a0e27 0%, #1a1f3a 8%, #1e2338 20%, #0f1419 100%),
    radial-gradient(circle at 20% 80%, rgba(59, 130, 246, 0.25) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(139, 92, 246, 0.22) 0%, transparent 50%),
    radial-gradient(circle at 40% 40%, rgba(16, 185, 129, 0.18) 0%, transparent 50%);
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 24px;
}

/* 科技感网格背景 */
#userLoginPage::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    linear-gradient(rgba(59, 130, 246, 0.15) 1px, transparent 1px),
    linear-gradient(90deg, rgba(59, 130, 246, 0.15) 1px, transparent 1px),
    linear-gradient(rgba(139, 92, 246, 0.12) 1px, transparent 1px),
    linear-gradient(90deg, rgba(139, 92, 246, 0.12) 1px, transparent 1px);
  background-size:
    100px 100px,
    100px 100px,
    20px 20px,
    20px 20px;
  pointer-events: none;
  animation: gridFloat 20s ease-in-out infinite;
  opacity: 0.6;
}

@keyframes gridFloat {
  0%,
  100% {
    transform: translate(0, 0);
  }
  50% {
    transform: translate(5px, 5px);
  }
}

#userLoginPage :deep(.ant-form) {
  max-width: 480px;
  width: 100%;
  background: rgba(15, 20, 35, 0.8);
  backdrop-filter: blur(20px);
  border-radius: 12px;
  border: 2px solid rgba(59, 130, 246, 0.3);
  box-shadow: 
    0 10px 40px rgba(0, 0, 0, 0.4),
    0 0 20px rgba(59, 130, 246, 0.2),
    inset 0 0 20px rgba(59, 130, 246, 0.1);
  padding: 32px;
  position: relative;
  z-index: 2;
}

.title {
  text-align: center;
  margin-bottom: 16px;
  color: rgba(255, 255, 255, 0.95);
  font-size: 28px;
  font-weight: 600;
  background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 50%, #10b981 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.desc {
  text-align: center;
  color: rgba(255, 255, 255, 0.7);
  margin-bottom: 24px;
}

.tips {
  text-align: right;
  color: rgba(255, 255, 255, 0.6);
  font-size: 13px;
  margin-bottom: 16px;
}

.tips a {
  color: #60a5fa;
  text-decoration: none;
}

.tips a:hover {
  color: #93c5fd;
  text-shadow: 0 0 10px rgba(59, 130, 246, 0.5);
}

#userLoginPage :deep(.ant-input),
#userLoginPage :deep(.ant-input-password) {
  background: rgba(20, 25, 40, 0.6);
  border: 1px solid rgba(59, 130, 246, 0.3);
  color: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
}

#userLoginPage :deep(.ant-input):focus,
#userLoginPage :deep(.ant-input-password .ant-input):focus {
  background: rgba(20, 25, 40, 0.8);
  border-color: rgba(59, 130, 246, 0.6);
  box-shadow: 0 0 15px rgba(59, 130, 246, 0.3);
}

#userLoginPage :deep(.ant-input::placeholder),
#userLoginPage :deep(.ant-input-password .ant-input::placeholder) {
  color: rgba(255, 255, 255, 0.5);
}

#userLoginPage :deep(.ant-btn-primary) {
  background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
  border: none;
  border-radius: 8px;
  height: 40px;
  font-weight: 500;
  box-shadow: 0 4px 15px rgba(59, 130, 246, 0.3);
  transition: all 0.3s;
}

#userLoginPage :deep(.ant-btn-primary:hover) {
  background: linear-gradient(135deg, #60a5fa 0%, #a78bfa 100%);
  box-shadow: 0 6px 20px rgba(59, 130, 246, 0.5);
  transform: translateY(-2px);
}
</style>
